package reduce.fz.com.reduce.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.VideoCardDetailActivity;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.VideoCard;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ScreenUtils;

/**
 *
 */
public class SocietyWorksFragment extends BaseListFragment<SocietyWorksFragment.SocietyWorkViewHolder, VideoCard> {

    private Long sid;

    public static SocietyWorksFragment create(Long sid) {
        SocietyWorksFragment fragment = new SocietyWorksFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("sid", sid);
        fragment.setArguments(bundle);
        return fragment;
    }

    public interface SocietyWorkItemListener {

        public void workClick(VideoCard video);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sid = getArguments().getLong("sid");
    }

    public SocietyWorksFragment() {
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
        return layoutManager;
    }

    public void forwardDetail(Long cardid) {
        Intent intent = new Intent(getActivity(), VideoCardDetailActivity.class);
        intent.putExtra("cardid", intent);
        startActivity(intent);
    }

    @Override
    protected SocietyWorkViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_society_work, parent, false);
        SocietyWorkViewHolder viewHolder = new SocietyWorkViewHolder(view, new SocietyWorkItemListener() {
            @Override
            public void workClick(VideoCard video) {
                forwardDetail(video.cardid);
            }
        });
        return viewHolder;
    }

    @Override
    protected SocietyWorkViewHolder createfootViewHolder(View view) {
        return new SocietyWorkViewHolder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List<VideoCard> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_society_work, sid,pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<VideoCard>>(new TypeReference<Result<List<VideoCard>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                loadingDialog.dismiss();
            }

            @Override
            public void responseInfo(Result<List<VideoCard>> data) {
                addResult(data);
            }
        });
    }

    @Override
    protected int getPageType() {
        return 0;
    }


    public static class SocietyWorkViewHolder extends BaseRecycleViewHolder<VideoCard> {
        private NetworkImageView mThum;
        private int height;
        private int margin;
        Boolean isFirstLine = false;
        Boolean isFristRow = false;
        private SocietyWorkItemListener listener;

        public SocietyWorkViewHolder(View itemView, SocietyWorkItemListener listener) {
            this(itemView);
            this.listener = listener;
        }

        public SocietyWorkViewHolder(View itemView) {
            super(itemView);
            margin = itemView.getResources().getDimensionPixelSize(R.dimen.dimen_18);
            mThum = (NetworkImageView) itemView.findViewById(R.id.iv_video_thum);
        }

        public int getWidth() {
            int width = (ScreenUtils.getScreenW() - 2 * margin) / 3;
            return width;
        }


        public void checkPosition(int position) {
            //默认非第一行，非第一列
            if (position <= 2)
                isFirstLine = true;
            if (position % 3 == 0)
                isFristRow = true;
        }

        public void resetMargin(int position) {
            int height=getWidth();
            int paddingtop = 0;
            int paddingbottom = 0;
            int paddingleft = 0;
            int paddingright = 0;
            if (isFirstLine) {
                paddingtop = 0;
                paddingbottom = 0;
            } else {
                paddingtop = margin;
                paddingbottom = 0;
            }
            if (isFristRow) {
                paddingleft = 0;
                paddingright = 0;
            } else {
                paddingright = 0;
                paddingleft = margin;
            }
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(height, height);
            lp.setMargins(paddingleft, paddingtop, paddingright, paddingbottom);
            mThum.setLayoutParams(lp);

        }

        @Override
        public void renderView(VideoCard data, int position) {
            checkPosition(position);
            resetMargin(position);
            mThum.setImageUrl(data.video.thumUrl, HttpUtils.getImageLoader());

        }
    }


}
